package com.neu.cloudFactory.service;

import com.neu.cloudFactory.DAO.ProductMsgDAO;
import com.neu.cloudFactory.DAO.ProductTypeDAO;
import com.neu.cloudFactory.PO.EquipType;
import com.neu.cloudFactory.PO.ProductMsg;
import com.neu.cloudFactory.PO.ProductType;
import com.neu.cloudFactory.VO.ProductMsgVO;
import com.neu.cloudFactory.VO.ProductTypeVO;
import com.neu.cloudFactory.VO.UserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductService {
    final ProductMsgDAO productMsgDAO;
    final ProductTypeDAO productTypeDAO;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    public ProductService(ProductMsgDAO productMsgDAO, ProductTypeDAO productTypeDAO) {
        this.productMsgDAO = productMsgDAO;
        this.productTypeDAO = productTypeDAO;
    }
    public boolean isExist(ProductMsg productMsg){
        return null!=productMsgDAO.findByName(productMsg.getName());
    }
    //新增或修改
    public void saveProduct(ProductMsg productMsg){
        productMsgDAO.save(productMsg);
    }
    public void saveProductType(ProductType productType){
        productTypeDAO.save(productType);
    }
    //列出所有产品
    public List<ProductMsgVO> findAllProduct(){
        List<ProductMsg> productMsgList = productMsgDAO.findAll();
        List<ProductMsgVO> productMsgVOList = new ArrayList<>();
        for(ProductMsg productMsg :productMsgList ){
            productMsgVOList.add(new ProductMsgVO(
                    productMsg,productTypeDAO.getById(productMsg.getTypeId())));
        }
        return productMsgVOList;
    }
    //列出所有产品种类
    public List<ProductTypeVO> findAllProductType(){
        return productTypeDAO.findAll().stream().map(ProductTypeVO::new).collect(Collectors.toList());
    }
    //删除产品
    public void deleteProduct(ProductMsg productMsg){
        productMsgDAO.delete(productMsg);
    }
    //删除产品种类
    public void deleteProductType(ProductType productType){
        productTypeDAO.delete(productType);
    }
    //类别是否被占用
    public boolean isOccupied (ProductType productType){
        return productMsgDAO.findByTypeId(productType.getId()).size()!=0;
    }

    public ProductType findProductTypeByName(String name){
        return productTypeDAO.findByName(name);
    }


    public ProductType findProductTypeById(int id){
        return productTypeDAO.getById(id);
    }

    public ProductMsg changeType(ProductMsgVO productMsgVO){
        ProductMsg productMsg = new ProductMsg();
        productMsg.setName(productMsgVO.getName());
        productMsg.setCode(productMsgVO.getCode());
        productMsg.setTypeId(productTypeDAO.findByName(productMsgVO.getType()).getId());
        productMsg.setDetail(productMsgVO.getDetail());
        return productMsg;
    }

    public ProductMsgVO addIdToPro(ProductMsg productMsg){//将id添加到要返回的VO上
        return new ProductMsgVO(
                productMsgDAO.findByName(productMsg.getName()),productTypeDAO.getById(productMsg.getTypeId()));
    }
    //自动生成编号
    public String generateCode() {
        Date date = new Date();
        return "PD" + sdf.format(date);
    }

}
